package com.shuoyue.ycgk.ui.system;


import android.content.Context;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.address.AddressBean;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.exception.NetErrorException;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.ArrayList;
import java.util.List;

import cn.addapp.pickers.entity.City;
import cn.addapp.pickers.entity.County;
import cn.addapp.pickers.entity.Province;
import io.reactivex.Observable;

public interface SystemAddressContract {
    class Model {
        public Observable<BaseResult<List<AddressBean>>> getArea(int pid) {
            return RetrofitClient.getInstance().getApi().getSysRegionList(pid);
        }

        public Observable<BaseResult<List<List<String>>>> getAllArea() {
            return RetrofitClient.getInstance().getApi().getAllSystemAddress();
        }
    }

    interface View extends BaseView {

        void setAreas(List<AddressBean> areas, int pid, int desLevel);

        void setAreas(ArrayList<Province> provinces);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getArea(int pid, int desLevel) {
            apply(model.getArea(pid))
                    .subscribe(new ApiSubscriber<Optional<List<AddressBean>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<AddressBean>> listOptional) {
                            super.onNext(listOptional);
                            if (listOptional.getIncludeNull().size() == 0) {
                                mView.onError(new NetErrorException("暂无地址数据", 499));
                            } else
                                mView.setAreas(listOptional.getIncludeNull(), pid, desLevel);
                        }
                    });
        }

        public void getAllArea(Context context) {
            apply(model.getAllArea())
                    .subscribe(new ApiSubscriber<Optional<List<List<String>>>>(mView, this,true) {
                        @Override
                        public void onNext(Optional<List<List<String>>> listOptional) {
                            super.onNext(listOptional);
                            if (listOptional.getIncludeNull().size() == 0) {
                                mView.onError(new NetErrorException("暂无地址数据", 499));
                            } else {
                                mView.setAreas(makeNodes(listOptional.getIncludeNull()));
                            }
                        }
                    });
        }

        private ArrayList<Province> makeNodes(List<List<String>> addressData) {
            //pid = 0是第一级
            ArrayList<Province> addressDatas = new ArrayList<>();
            for (List<String> item : addressData) {
                if(item.size()!=3){
                    continue;
                }
                if (item.get(1).equals("0")) {
                    Province province = new Province();
                    province.setAreaId(item.get(0));
                    province.setAreaName(item.get(2));
                    province.setCities(new ArrayList<>());
                    addressDatas.add(province);
                    for (List<String> itemCity : addressData) {
                        if (itemCity.get(1).equals(item.get(0))) {
                            City city = new City();
                            city.setAreaId(itemCity.get(0));
                            city.setAreaName(itemCity.get(2));
                            city.setCounties(new ArrayList<>());
                            province.getCities().add(city);
                            for (List<String> itemCountry : addressData) {
                                if (itemCountry.get(1).equals(itemCity.get(0))) {
                                    County county = new County();
                                    county.setAreaId(itemCountry.get(0));
                                    county.setAreaName(itemCountry.get(2));
                                    city.getCounties().add(county);
                                }
                            }


                        }
                    }

                }
            }
            return addressDatas;
        }
    }


}
